<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="入住角色" prop="role">
        <el-select
          v-model="queryParams.role"
          placeholder="请选择入住角色"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.ACCOMMODATION_ROOM_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入住人" prop="resideName">
        <el-input
          v-model="queryParams.resideName"
          placeholder="请输入入住人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="房间名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入房间名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="人员名称" prop="resideName">
        <el-input
          v-model="queryParams.resideName"
          placeholder="请输入人员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="当前状态" prop="resideStatus">
        <el-select
          v-model="queryParams.resideStatus"
          placeholder="请选择入住当前状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in this.getDictDatas(
              DICT_TYPE.ACCOMMODATION_ROOM_STATUS
            )"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="学院" prop="collegeId">
        <college-selector v-model="queryParams.collegeId"></college-selector>
      </el-form-item>
      <el-form-item label="系部" prop="departmentId">
        <departments-selector
          v-model="queryParams.departmentId"
          :college-id="queryParams.collegeId"
        ></departments-selector>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          @click="handleAdd"
          v-hasPermi="['collegebase:accommodation-reside:create']"
          >添加入住</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          @click="handleMoveIn"
          v-hasPermi="['collegebase:accommodation-reside:create']"
          >迁入</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['collegebase:accommodation-reside:export']"
          >导出</el-button
        >
      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      highlight-current-row
      @row-click="rowClick"
    >
      <el-table-column label width="35">
        <template slot-scope="scope">
          <el-radio :label="scope.row.id" v-model="sourceId"></el-radio>
        </template>
      </el-table-column>
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column
        label="床铺名称"
        v-if="columns[0].visible"
        align="center"
        prop="bedName"
        show-overflow-tooltip
      />
      <el-table-column
        label="学院"
        v-if="columns[1].visible"
        align="center"
        prop="collegeName"
        show-overflow-tooltip
      />
      <el-table-column
        label="系部"
        v-if="columns[2].visible"
        align="center"
        prop="departmentName"
        show-overflow-tooltip
      />
      <el-table-column
        label="房间名称"
        v-if="columns[3].visible"
        align="center"
        prop="roomName"
        show-overflow-tooltip
      />
      <el-table-column
        label="人员姓名"
        v-if="columns[4].visible"
        align="center"
        prop="resideName"
        show-overflow-tooltip
      />
      <el-table-column
        label="联系方式"
        v-if="columns[5].visible"
        align="center"
        prop="residePhone"
        show-overflow-tooltip
      />
      <el-table-column
        label="状态"
        align="center"
        v-if="columns[6].visible"
        prop="resideStatus"
        key="6"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <dict-tag
            :type="DICT_TYPE.ACCOMMODATION_ROOM_STATUS"
            :value="scope.row.resideStatus"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="入住人id" align="center" prop="resideId" /> -->
      <!-- <el-table-column label="入住角色（角色+id 联合确立关系）" align="center" prop="role">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.ACCOMMODATION_ROOM_TYPE" :value="scope.row.role" />
        </template>
      </el-table-column> -->

      <!-- <el-table-column label="入住备注" align="center" prop="resideRemark" /> -->

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="165"
        v-if="columns[7].visible"
        key="7"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['collegebase:accommodation-reside:update']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleMove(scope.row)"
            v-hasPermi="['collegebase:accommodation-reside:update']"
            >迁出</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加修改组件 -->
    <form-edit
      :show.sync="open"
      :title="title"
      @success="getList"
      ref="editDialog"
    ></form-edit>
    <move-in
      :show.sync="openMoveIn"
      title="迁入"
      @success="getList"
      ref="moveInDialog"
    />
  </div>
</template>

<script>
import {
  createAccommodationReside,
  updateAccommodationReside,
  deleteAccommodationReside,
  getAccommodationReside,
  getAccommodationResidePage,
  exportAccommodationResideExcel,
  moveOut,
} from "@/api/collegebase/accommodationReside";
import formEdit from "./formEdit";
import moveIn from "./moveIn.vue";
import common from "@/api/collegebase/common";
import CollegeSelector from "@/components/CollegeSelector";
import DepartmentsSelector from "@/components/DepartmentsSelector";
export default {
  name: "AccommodationReside",
  components: {
    DepartmentsSelector,
    CollegeSelector,
    formEdit,
    moveIn,
  },
  data() {
    return {
      columns: [
        { key: 0, label: "床铺名称", visible: true },
        { key: 1, label: "学院", visible: true },
        { key: 2, label: "系部", visible: true },
        { key: 3, label: "房间名称", visible: true },
        { key: 4, label: "人员姓名", visible: true },
        { key: 5, label: "联系方式", visible: true },
        { key: 6, label: "状态", visible: true },
        { key: 7, label: "创建时间", visible: false },
      ],
      openMoveIn: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入住记录列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      // <!-- roomName resideName roomStatus-->
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        role: null,
        resideName: null,
        bedId: null,
        collegeId: null,
        roomName: null,
        resideStatus: null,
        departmentId: null
      },
      sourceId: '',
      rowData: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    rowClick(row) {
      this.rowData = row;
      // console.log(row)
      this.sourceId = row.id;
      console.log(this.sourceId)
      // console.log('exportid---', row.snapshotId)

    },

    handleMoveIn() {
      if (!this.sourceId) {
        return this.$message.error('请选择一条数据')
      }


      //  this.form = this.rowData;
      getAccommodationReside(this.rowData.id).then((response) => {
        let form = response.data;
        this.openMoveIn = true;
        ;
        this.$refs.moveInDialog.initData(form)

      });



    },
    handleMove(row) {
      console.log(row);
      moveOut({ id: row.id }).then((res) => {
        if (res.data) {
          this.$message.success("迁出成功");
          this.getList();
        }
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getAccommodationResidePage(this.queryParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加入住记录";
      this.$refs.editDialog.initData({});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id;
      getAccommodationReside(id).then((response) => {
        let form = response.data;
        this.open = true;
        this.title = "修改入住记录";
        this.$refs.editDialog.initData(form);
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除入住记录人员名称为"' + row.resideName + '"的数据项?')
        .then(function () {
          return deleteAccommodationReside(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出所有入住记录数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportAccommodationResideExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "入住记录.xls");
          this.exportLoading = false;
        })
        .catch(() => { });
    },
  },
};
</script>
